package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.model.DemandCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.DemandCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:51
 * 
 * 需求类别管理类
 */
@RestController
@RequestMapping("/demandCategory/")
public class DemandCategoryController {
    
    @Autowired
    DemandCategoryService demandCategpryService;

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回chang
     * @param search 搜索，如果无搜索此值为paging
     * @param sort time
     * @param way desc，asc，noway
     */
    @GetMapping("/list/{page}/{size}/{draw}/{search}/{sort}/{way}")
    public Map<String, Object> list(@PathVariable("page") Integer page,
                                        @PathVariable("size") Integer size,
                                        @PathVariable("draw") String draw,
                                        @PathVariable("search") String search,
                                        @PathVariable("sort") String sort,
                                        @PathVariable("way") String way){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        List<DemandCategory> list = demandCategpryService.selectAllByDemandCategoryDel(0,search, sort, way);
        PageInfo<DemandCategory> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            map.put("pageData", pageInfo.getList());
            map.put("recordsTotal", pageInfo.getTotal());
            map.put("draw", draw);
            map.put("pages", pageInfo.getPages());
            map.put("status",200);
        }else{
            map.put("status",401);
            map.put("msg","暂无数据");
        }
        return map;
    }

    /**
     * 添加需求类别
     * @param demandCategory
     * @return
     */
    @PostMapping("/save")
    public Response save(DemandCategory demandCategory){
        return demandCategpryService.save(demandCategory);
    }

    /**
     * 修改需求类别
     * @param demandCategory
     * @return
     */
    @PutMapping("/update")
    public Response update(DemandCategory demandCategory){
        return demandCategpryService.update(demandCategory);
    }

    /**
     * 查询需求类别详情
     * @param demandCategoryId:需求类别的id
     * @return
     */
    @GetMapping("/getDemandCategoryById/{id}")
    public Response getShopCategoryById(@PathVariable("id")  String demandCategoryId){
        return demandCategpryService.getDemandCategoryById(demandCategoryId);
    }

    /**
     * 删除
     * @param demandCategoryId:需求类别的id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id")  String demandCategoryId){
        return demandCategpryService.deleteById(demandCategoryId);
    }

    /**
     * 批量删除
     * @param id:需求分类的id集合
     * @return
     */
    @DeleteMapping("/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return demandCategpryService.batchDeleteById(id);
    }

    /**
     * 更改需求类别的状态
     * @param demandCategoryId:需求类别的id
     * @return
     */
    @GetMapping("/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id")  String demandCategoryId,
                                            @PathVariable("status") Integer status){
        return demandCategpryService.changeStatus(demandCategoryId, status);
    }
    
    
}
